﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace GSF.Objects
{
    class Objects
    {
        public bool isVisible;

        protected Texture2D m_Texture2D;

        public Rectangle m_desRectangle;
        protected Rectangle m_srcRectangle;

        protected Vector2 m_Position;
        protected Vector2 m_Direction;

        protected int m_currentFrame;
        protected int m_Colums;
        protected int m_Rows;
        protected int m_Width;
        protected int m_Height;
        protected int m_maxX;
        protected int m_maxY;

        public Vector2 Position
        {
            get
            {
                return m_Position;
            }
            set
            {
                m_Position = value;
            }
        }
        public Vector2 Direction
        {
            get
            {
                return m_Direction;
            }
            set
            {
                m_Direction = value;
            }
        }

        public int Height
        {
            set
            {
                m_Height = value;
            }
            get
            {
                return m_Height;
            }
        }
        public int Width
        {
            set
            {
                m_Width = value;
            }
            get
            {
                return m_Width;
            }
        }

        public Vector2 CenterPoint
        {
            get
            {
                return new Vector2(m_desRectangle.X + m_Width / 2, m_desRectangle.Y + m_Height / 2);
            }
        }

        public Objects(Game game,string texture2d, int m_Rows, int cols)
        {
            isVisible = false;
            this.m_Texture2D = game.Content.Load<Texture2D>(texture2d);
            this.m_Colums = cols;
            this.m_Rows = m_Rows;

            this.m_Width = m_Texture2D.Width / cols;
            this.m_Height = m_Texture2D.Height / m_Rows;

            m_maxX = Global.GRAPHICS.GraphicsDevice.Viewport.Width;
            m_maxY = Global.GRAPHICS.GraphicsDevice.Viewport.Height;

            m_Position = new Vector2(0, 0);
            m_Direction = new Vector2(1, 0);

            m_currentFrame = 0;
        }
        public virtual void update()
        {
            m_currentFrame++;
            if (m_currentFrame >= m_Colums)
                 m_currentFrame = 0;
            setRec();
        }

        protected virtual void setRec()
        {
            int row = (int)((float)m_currentFrame / (float)m_Colums);
            int colum = m_currentFrame % m_Colums;
            m_srcRectangle = new Rectangle(m_Width * colum, m_Height * row, m_Width, m_Height);
            m_desRectangle = new Rectangle((int)m_Position.X, (int)m_Position.Y, m_Width, m_Height);
        }

        public virtual void draw(SpriteBatch sprite)
        {
            sprite.Draw(m_Texture2D, m_desRectangle, m_srcRectangle, Color.White);
        }
    }
}
